package com.we823.cms.repository.service.beetl.tag;

import com.github.pagehelper.PageInfo;
import com.we823.cms.model.extension.CaseCategory;
import com.we823.cms.model.extension.CaseModel;
import com.we823.cms.repository.service.extension.CaseCategoryService;
import com.we823.cms.repository.service.extension.CaseService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("cms_cases")
@Scope("prototype")
public class CaseListTag extends AbstractListTag {

	private static String PARAM_CATEGORY_ID = "category_id";
	private static String PARAM_SHOW_THUMB="thumb";

	private @Resource CaseService caseService;
	private @Resource CaseCategoryService caseCategoryService;

	@Override
	public void execute() {
		int categoryId = getParam(PARAM_CATEGORY_ID, 0);
		/**
		 * 首页是否显示图片
		 */
		int thumb = getParam(PARAM_SHOW_THUMB, 0);
		
		List<CaseModel> caseModels = null;
		if(categoryId==0){
			if(pageRequest==null){
				if (thumb>0) {
					caseModels = caseService.findByThumbNotNull(sort);
				}else {
					caseModels = caseService.findAll(sort);
				}
				
			}else{
				PageInfo<CaseModel> casePage;
				if(thumb>0) {
					casePage = caseService.findByThumbNotNull(pageRequest);
				}else {
					casePage = caseService.findAll(pageRequest);
				}
				if(casePage!=null){
					caseModels = casePage.getList();
				}
			}
		}else{
			CaseCategory caseCategoryModel = caseCategoryService.findOne(categoryId);
			if(pageRequest==null){
				if (thumb>0) {
					caseModels = caseService.findByCategoryAndThumbNotNull(caseCategoryModel,sort);
				}else {
					caseModels = caseService.findByCategory(caseCategoryModel, sort);
				}
			}else{
				PageInfo<CaseModel> casePage;
				if(thumb>0) {
					casePage = caseService.findByCategoryAndThumbNotNull(caseCategoryModel, pageRequest);
				}else{
					casePage = caseService.findByCategory(caseCategoryModel, pageRequest);
				}
				if(casePage!=null){
					caseModels = casePage.getList();
				}
			}
		}
		
		this.binds(caseModels);

	}
}
